#include <windows.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
     HANDLE console = GetStdHandle(STD_INPUT_HANDLE);
     DWORD mode;
     GetConsoleMode(console, &mode);
     SetConsoleMode(console, mode & ~ENABLE_LINE_INPUT);
     cin >> noskipws;

     srand((int)time(0));

     char answer;
     int player_hit_points = 20;
     int player_initiative = 4;
     int player_AC = 20;
     int player_reflex = 15;
     int player_bullets = 1;
     int player_pistol_attack_bonus = 5;
     int player_bullet_damage_bonus = 10;
     int player_bullet_damage_dice = 10;
     int player_punch_attack_bonus = 15;
     int player_punch_damage_dice = 3;
     int player_punch_damage_bonus = 3;

     int dragon_hit_points = 20;
     int dragon_initiative = 8;
     int dragon_AC = 20;
     int dragon_claw_attack_bonus = 8;
     int dragon_claw_damage_dice = 10;
     int dragon_claw_damage_bonus = 5;
     int dragon_breath_attack_bonus = 8;
     int dragon_breath_damage_dice = 10;
     int dragon_breath_damage_bonus = 10;
     bool used_fire_breath = false;

     cout << "A dragon!" << endl;
     cout << endl;

     bool do_combat = true;

     bool player_turn = (rand() % 20 + 1 + player_initiative > rand() % 20 + 1 + dragon_initiative);

     do {

          if (player_turn) {

               do {
                    cout << "Will you:" << endl;
                    cout << "[s] shoot at the dragon," << endl;
                    cout << "[p] punch the dragon," << endl;
					cout << "[d] fight dirty," << endl;
                    cout << "[r] run away." << endl;
                    cout << endl;
                    cout << ">";

                    cin >> answer;

                    switch (answer) {

                    case 's':
                         if (player_bullets > 0) {

                              cout << endl;
                              cout << endl;
                              cout << "You shoot at the dragon with your pistol..." << endl;
                              if (rand() % 20 + 1 + player_pistol_attack_bonus >= dragon_AC) {

                                   cout << "Your bullet pierces the dragon." << endl;
                                   dragon_hit_points -= rand() % player_bullet_damage_dice + 1 + player_bullet_damage_bonus;

                              } else {

                                   cout << "Your shot bounces off the dragon's tough scales." << endl;
                              }
                              player_bullets--;

                         } else {

                              cout << endl;
                              cout << endl;
                              cout << "You are out of bullets!" << endl;
                              answer = '?';
                         }
                         break;

                    case 'p':
                         cout << "You attempt to punch the dragon in the face..." << endl;
                         if (rand() % 20 + 1 + player_punch_attack_bonus >= dragon_AC) {

                              cout << "your punch connects." << endl;
                              dragon_hit_points -= rand() % player_punch_damage_dice + 1 + player_punch_damage_bonus;

                         } else {

                              cout << "your swing misses." << endl;
                         }
                         break;

					case 'f':
						cout << endl;
						cout << "the dragon rears but you telegraph his move," << endl;
						cout << "seeing the opportunity open you react quickly," << endl;
						cout << "kicking up dirt  at his face..." << endl;

						if (rand() % 20 + 1 + player_punch_attack_bonus >= dragon_AC)



                    case 'r':
                         cout << endl;
                         cout << "The dragon takes another swipe as you flee..." << endl;

                         if (rand() % 20 + 1 + dragon_claw_attack_bonus + 2 >= player_AC - 5) {

                              cout << "the claws penetrate your armour and bite into your flesh." << endl;
                              player_hit_points -= rand() % dragon_claw_damage_dice + 1 + dragon_claw_damage_bonus;

                         } else {

                              cout << "the claws scratch your armour but you are unharmed." << endl;
                         }
                         cout << endl;
                         do_combat = false;
                         break;

                    default:
                         cout << endl;
                         cout << endl;
                         cout << "I didn't understand that." << endl;
                         cout << endl;
                         answer = '?';
                         break;
                    }

               } while (answer == '?');

          } else { // dragon's turn

               if (!used_fire_breath && rand() % 2 == 0) {
               
                    cout << "The dragon breathes fire at you..." << endl;

                    if (rand() % 20 + 1 + dragon_breath_attack_bonus >= player_reflex) {

                         cout << "and you are burned." << endl;
                         player_hit_points -= rand() % dragon_breath_damage_dice + 1 + dragon_breath_damage_bonus;

                    } else {

                         cout << "but you manage to dodge the blast." << endl;
                    }
                    used_fire_breath = true;
               
               } else {

                    cout << "The dragon takes a swipe at you with its massive claws..." << endl;

                    if (rand() % 20 + 1 + dragon_claw_attack_bonus >= player_AC) {

                         cout << "the claws penetrate your armour and bite into your flesh." << endl;
                         player_hit_points -= rand() % dragon_claw_damage_dice + 1 + dragon_claw_damage_bonus;

                    } else {

                         cout << "the claws scratch your armour but you are unharmed." << endl;
                    }
               }
          }

          player_turn = !player_turn;

          if (player_hit_points < 1 || dragon_hit_points < 1) {

               do_combat = false;
          }

     } while (do_combat);

     if (player_hit_points < 1) {

          cout << "You died!" << endl;

     } else if (dragon_hit_points < 1) {

          cout << "You defeated the dragon!" << endl;

     } else {

          cout << "You live to fight another day..." << endl;
     }

     cin >> answer; // wait for keypress to end

     return 0;
}
